package service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import db.ConnectDB;

import entity.Member;

//执行插入Member表的服务类
public class MemberService {
	/**
	 * @param member 加入的会员
	 * @return void
	 */
	public void addMember(Member member){
		String name = member.getName();
		String address = member.getAddress();
		String job = member.getJob();
		Connection conn = null;
		//执行sql的Statement
		Statement stmt;
		conn = ConnectDB.getConnection();
		try{
			stmt = conn.createStatement();
			//执行SQL的结果集
			ResultSet rs ;
			rs = stmt.executeQuery("select * from member;");
			rs.last();
			
			//加入的会员的会员编号
			int memberId = rs.getRow() + 1;
			//把会员信息插入会员表
			stmt.execute("insert into member (memberId,name,address,job) values ("+ memberId +
					  "," + '\'' + name + '\'' +","+ '\'' + address+ '\'' + ","+ '\'' + job+  '\'' +
					  ");");
			System.out.println("OK. " + name + "'s member id is "+ ("00"+memberId).substring(("00"+memberId).length()-3)+".");
		}catch(SQLException sqle){
			sqle.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
				
	}
}
